package br.gov.ibama.client.visao;

import java.util.List;

import br.gov.ibama.client.componentes.BasisComponentes;
import br.gov.ibama.client.componentes.FormularioBasis;
import br.gov.ibama.client.servico.ServicoOperacoesCRUD;

import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.ImgButton;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.CloseClickEvent;
import com.smartgwt.client.widgets.events.CloseClickHandler;
import com.smartgwt.client.widgets.events.DragMoveEvent;
import com.smartgwt.client.widgets.events.DragMoveHandler;
import com.smartgwt.client.widgets.events.MaximizeClickEvent;
import com.smartgwt.client.widgets.events.MaximizeClickHandler;
import com.smartgwt.client.widgets.events.RestoreClickEvent;
import com.smartgwt.client.widgets.events.RestoreClickHandler;
import com.smartgwt.client.widgets.form.fields.FormItem;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.toolbar.ToolStrip;

public abstract class VisaoPopup extends Window {

	private static final int LARGURA_10 = 10;
	private static final int LARGURA_40 = 40;
	private static final String STYLE = "style";
	private static final String STYLE_NOSTYLE = "_nostyle";
	public static final int WIDTH = 600;
	public static final int HEIGHT = 440;

	public static final int ESTADO_EDITAR = 1;
	public static final int ESTADO_VISUALIZAR = 2;
	public static final int ESTADO_SALVAR = 3;

	private HLayout janela;
	private VLayout barra;
	private Window conteudo;
	private ToolStrip barraDeBotoes;

	public FormularioBasis formulario;


	public VisaoPopup(String title) {
		this.setTitle(title);
		configuraPopup();
		criaFormulario();
		addComponentes();
	}

	private void configuraPopup() {
		setConfiguracaoBasis();
		criaComponentes();
		montarPainel();
	}

	protected void criaCamposVisualizacao() {

	}

	private void setConfiguracaoBasis() {
		this.setWidth(WIDTH);
		this.setHeight(HEIGHT);
		this.setShowMinimizeButton(false);
		this.setShowMaximizeButton(true);
		this.setShowCloseButton(true);
		this.setIsModal(true);
		this.setShowModalMask(true);
		this.centerInPage();
		this.setShowHeader(false);
		this.setShowFooter(false);
		this.setPadding(0);
		this.setCanDragResize(true);
		this.setCanDrag(false);
		this.setBorder(null);
		this.setAlign(VerticalAlignment.CENTER);
		this.addStyleName(STYLE_NOSTYLE);
		this.setBodyStyle(STYLE_NOSTYLE);
		this.setAttribute(STYLE, " ", true);
	}

	private VisaoPopup retornaPopUp() {
		return this;
	}

	private void criaComponentes() {
		criaJanela();
		criaConteudo();
		criaBarraLateral();
	}

	private void criaJanela() {
		janela = new HLayout();
		janela.setWidth100();
		janela.setHeight100();
		janela.setAlign(VerticalAlignment.CENTER);
		janela.setStyleName(STYLE_NOSTYLE);
	}

	private void criaConteudo() {
		conteudo = new Window();
		conteudo.setTitle(this.getTitle());
		conteudo.setWidth100();
		conteudo.setHeight100();
		conteudo.setPadding(LARGURA_10);
		conteudo.setPaddingAsLayoutMargin(true);
		conteudo.setLayoutAlign(Alignment.CENTER);
		conteudo.setAlign(Alignment.CENTER);
		conteudo.setShowMinimizeButton(false);
		conteudo.setShowMaximizeButton(true);
		conteudo.setShowCloseButton(true);
		conteudo.addCloseClickHandler(new CloseClickHandler() {
			public void onCloseClick(CloseClickEvent event) {
				retornaPopUp().destroy();
			}
		});
		conteudo.addMaximizeClickHandler(new MaximizeClickHandler() {

			public void onMaximizeClick(MaximizeClickEvent event) {
				retornaPopUp().maximize();
			}
		});
		conteudo.addRestoreClickHandler(new RestoreClickHandler() {

			public void onRestoreClick(RestoreClickEvent event) {
				retornaPopUp().restore();
			}
		});
		conteudo.addDragMoveHandler(new DragMoveHandler() {

			public void onDragMove(DragMoveEvent event) {
				retornaPopUp().addDragMoveHandler(this);
			}
		});
	}

	private void criaBarraLateral() {
		barra = new VLayout();
		barra.setWidth(LARGURA_40);
		barra.setHeight100();
		barra.setAlign(VerticalAlignment.CENTER);
		barra.setStyleName(STYLE_NOSTYLE);
		barraDeBotoes = BasisComponentes.criaBarraDeBotoesVertical("");
		List<ImgButton> botoes = getBotoes();
		barraDeBotoes.setMembers(botoes.toArray(new ImgButton[botoes.size()]));
		barra.setMembers(getBarraDeBotoes());
	}

	public VLayout getBarra() {
		return barra;
	}

	public void setBarra(VLayout barra) {
		this.barra = barra;
	}

	private void montarPainel() {
		janela.addMembers(getConteudo(), getBarra());
		this.addItem(getJanela());
	}

	private HLayout getJanela() {
		return janela;
	}
	
	private Window getConteudo() {
		return conteudo;
	}

	private ToolStrip getBarraDeBotoes() {
		return barraDeBotoes;
	}

	public void setaConteudoPopUp(Canvas conteudo) {
		getConteudo().addItem(conteudo);
	}

	protected abstract List<ImgButton> getBotoes();

	protected abstract void criaFormulario();

	public FormularioBasis getFormulario() {
		return formulario;
	}

	public void setFormulario(FormularioBasis formulario) {
		this.formulario = formulario;
	}
	
	public void preparaFormulario(String ds, FormItem[] fields) {
		formulario = new FormularioBasis();
		formulario.setDataSource(DataSource.get(ds));
		formulario.setPadding(LARGURA_10);
		formulario.setLayoutAlign(Alignment.LEFT);
		formulario.setFields(fields);
		this.setaConteudoPopUp(formulario);
		formulario.setServico(new ServicoOperacoesCRUD(formulario));
	}
	
	
	protected void addComponentes() {
		
	}
	
	/**
	 * 
	 */
	public void redefinirTamanhoJanelaPopUp(String altura, String largura, String larguraGrupo, String alturaGrupo){
		//Redefinição do formulário quanto a largura, altura, alinhamento e margem
		setHeight(altura);
		setWidth(largura);
		formulario.setAlign(Alignment.CENTER);
		formulario.setWidth(larguraGrupo);		
		formulario.setHeight(alturaGrupo);
	}
}
